﻿using System;
using System.Collections.Generic;
using System.Linq;

using KingsSharp.Core;
using KingsSharp.Core.Data;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;

using CRM.Admin.Contracts.Contract.Sys;
using CRM.Admin.Contracts.Model.Sys;

namespace Property.Main.Services.Service.Sys
{
    public class RoleService : ServiceBase, IRoleService
    {
        private IRepository<Role, Guid> RoleRepository;

        public RoleService(IRepository<Role, Guid> _RoleRepository)
            : base(_RoleRepository.UnitOfWork)
        {
            this.RoleRepository = _RoleRepository;
        }
        public IQueryable<Role> ItemRole
        {
            get { return RoleRepository.Entities; }
        }

        public KingsSharp.Common.Data.OperationResult InsertRole(params CRM.Admin.Contracts.Dto.Sys.RoleDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return RoleRepository.Insert(dtos);
        }

        public KingsSharp.Common.Data.OperationResult UpdateRole(params CRM.Admin.Contracts.Dto.Sys.RoleDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return RoleRepository.Update(dtos);
        }

        public KingsSharp.Common.Data.OperationResult DeleteRole(params Guid[] ids)
        {
            ids.CheckNotNull("ids");
            var entitys = ItemRole.Where(a => ids.Contains(a.Id)).ToList();
            entitys.ForEach(a => a.IsDeleted = true);
            int result = RoleRepository.Update(entitys.ToArray());
            return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
        }
    }
}
